package com.gopay.scheduler.service.cbrmb.impl;

import java.io.File;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.cbrmb.CbRemitFtpSetp;
import com.gopay.common.cps.dao.cbrmb.CbRemitFileUploadDtlDAO;
import com.gopay.common.cps.dao.cbrmb.CbRemitFtpFileDtlDAO;
import com.gopay.common.domain.cbrmb.CbRemitFileUploadDtl;
import com.gopay.common.domain.cbrmb.CbRemitFtpFileDtl;
import com.gopay.common.util.cbrmb.FtpUtils;
import com.gopay.scheduler.service.cbrmb.CbRmbRemitFtpFileService;
import com.ibm.icu.util.Calendar;

@Service("cbRmbRemitFtpFileService")
public class CbRmbRemitFtpFileServiceImpl implements CbRmbRemitFtpFileService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "cbRemitFileUploadDtlDAO")
	private CbRemitFileUploadDtlDAO cbRemitFileUploadDtlDAO;

	@Resource(name = "cbRemitFtpFileDtlDAO")
	private CbRemitFtpFileDtlDAO cbRemitFtpFileDtlDAO;

	@Resource(name = "cbrConfig")
	private Map<String, String> cbrConfig;

	@Override
	public void downloadFtpFile(CbRemitFtpFileDtl cbRemitFtpFileDtl) {
		cbRemitFtpFileDtl.setCount(cbRemitFtpFileDtl.getCount() + 1);
		cbRemitFtpFileDtl.setDownLoadState(CbRemitFtpSetp.DOWNLOAD_READING.value);
		cbRemitFtpFileDtl.setLastUpdTs(new Date());
		cbRemitFtpFileDtlDAO.update(cbRemitFtpFileDtl);
		String folderPath = getFolderPath(cbRemitFtpFileDtl.getCustId());
		String filePath = cbrConfig.get("cbrmb.remit.contract.url") + folderPath;
		// 创建文件路径
		File f = new File(filePath);
		if (!f.exists()) {
			f.mkdirs();
		}
		String fileName = System.currentTimeMillis() + "-" + cbRemitFtpFileDtl.getOriFileName();
		String fileAddr = filePath + fileName;
		FtpUtils ftp = new FtpUtils(cbrConfig.get("ftp.ip"), Integer.valueOf(cbrConfig.get("ftp.port")),
				cbrConfig.get("ftp.username"), cbrConfig.get("ftp.password"),
				cbrConfig.get("cbr.ftp.remit.file.path"));
		boolean result = false;
		logger.error("文件地址为：" + fileAddr + ",filepath:" + folderPath + fileName);
		try {
			result = ftp.downLoadFile(cbRemitFtpFileDtl.getFileFtpAddr(), cbRemitFtpFileDtl.getOriFileName(), fileAddr);
		} catch (Exception e) {
			logger.error("ftp文件下载异常:", e);
			if (cbRemitFtpFileDtl.getCount() == 7) {
				cbRemitFtpFileDtl.setDownLoadState(CbRemitFtpSetp.DOWNLOAD_FAIL.value);
			} else {
				cbRemitFtpFileDtl.setDownLoadState(CbRemitFtpSetp.DOWNLOAD_READY.value);
			}
			cbRemitFtpFileDtl.setLastUpdTs(new Date());
			cbRemitFtpFileDtlDAO.update(cbRemitFtpFileDtl);
		}
		if (result) {
			CbRemitFileUploadDtl cbRemitFileUploadDtl = cbRemitFileUploadDtlDAO.find(CbRemitFileUploadDtl.class,
					cbRemitFtpFileDtl.getFileId());
			cbRemitFileUploadDtl.setLastUpdTs(new Date());
			cbRemitFileUploadDtl.setFileUrl(folderPath + fileName);
			cbRemitFileUploadDtl.setFileAddr(fileAddr);
			cbRemitFileUploadDtlDAO.update(cbRemitFileUploadDtl);
			cbRemitFtpFileDtl.setDownLoadState(CbRemitFtpSetp.DOWNLOAD_READIED.value);
			cbRemitFtpFileDtl.setLastUpdTs(new Date());
			cbRemitFtpFileDtlDAO.update(cbRemitFtpFileDtl);
		}

	}

	// 拼接文件保存路径
	private String getFolderPath(String custId) {
		Calendar c = Calendar.getInstance();
		String mm = String.valueOf(c.get(Calendar.MONTH) + 1);
		mm = mm.length() > 1 ? mm : "0" + mm;
		String dd = String.valueOf(c.get(Calendar.DATE) + 1);
		dd = dd.length() > 1 ? dd : "0" + dd;
		String folderPath = File.separator + c.get(Calendar.YEAR) + mm + dd + File.separator + custId + File.separator;
		return folderPath;
	}

}
